<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 照片墙
 * @author 王春林
 */
class photo_model extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	//获取收藏的照片
	function listCollectImg($data,$beginNumber=-1,$pageSzie=55) {
		$userId = $data['userId'];
		$queryString = "SELECT * FROM wanwan_enum_collection WHERE userId = $userId";
		$rs = $this->db->query($queryString);
		return $rs->result_array();
	}
	
	//获取好友近期照片
	function listFriendsImg($data,$beginNumber=-1,$pageSize=55) {
		$userId = $data['userId'];
		$queryString = " SELECT * FROM wanwan_enum_photowall WHERE userId IN 
		(SELECT userId FROM wanwan_enum_usertoteam WHERE teamId IN(SELECT teamId FROM wanwan_enum_usertoteam WHERE userId= $userId ))
		ORDER BY createTime desc"; 
		$rs = $this->db->query($queryString);
		return $rs->result_array();
	}
	
	//获取上传的照片
	function listUploadImg($data, $beginNumber=-1, $pageSize=55) {
		$limit =($beginNumber==-1)?"":"limit $beginNumber,$pageSize";
		$userId = $data ['userId'];
		$sql = "SELECT id,userId,imgSrc,createTime,description,'dynamic' type FROM wanwan_enum_userdynamic WHERE userId=$userId  AND TYPE='image' 
			UNION ALL SELECT id,userId,imgSrc,createTime,description,'photowall' type FROM wanwan_enum_photowall WHERE userId=$userId order by createTime desc $limit";
		$rs = $this->db->query ( $sql )->result_array ();
		foreach ( $rs as $key => $row ) {
			if ($row['type']=='dynamic') {
				$arrayImg= explode ( ",", $row['imgSrc'] );
				unset($rs[$key]);
				foreach ( $arrayImg as $val ) {
					if (! empty ( $val )) {
						$row ['imgSrc'] = $val;
						array_push($rs,$row);
					}
				}
			}
		}
		return $rs;
	}
	
	function getPreviewImages($data,$type="Upload",$beginNumber=-1,$pageSize=55){
		$funcStr		= 'list'.$type."Img";
		$tableResult	= $this->$funcStr($data,$beginNumber,$pageSize);
		return  $tableResult;
		
	}
}
?>